// pages/member/notice.js
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    dataList: [],
    // 显示加载更多 loading
    hothidden: true,
    loadinghidden: true,
    isAddBtnShow: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    /**
     * 获取系统信息
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    that.doLoadData();
  },
  onShow: function () {
    var that = this;
    var refreshList = wx.getStorageSync("refreshList");
    if (refreshList == "notice") {
      that.doLoadData();
      wx.setStorageSync("refreshList", "");
    }
  },
  //加载数据
  doLoadData: function () {
    var that = this;

    var parameter = {};
    parameter['page'] = 1;

    /**
     * 显示 loading
     */
    that.setData({
      page: 1,
      loadinghidden: false,
      hothidden: false,
    });

    http.ApiGet(apiUrl.UrlNoticeList, parameter, function (res) {
      wx.stopPullDownRefresh(); //停止下拉刷新
      wx.hideNavigationBarLoading(); //完成停止加载
      that.setData({
        loadinghidden: true
      })
      if (res.result.errorCode == 1) {
        var dataList = res.data;
        if (dataList.length > 0) {
          dataList = dataList.map(function (item) {

            let start_time = (new Date().getTime() / 1000) - item.start_time;

            if (start_time < 0) {
              item.start_time_text += '(未开始)';
            }

            let end_time = (new Date().getTime() / 1000) - item.end_time;
            if (end_time > 0) {
              item.end_time_text += '(已结束)';
            }

            return item;
          })
        }

        that.setData({
          page: 1,
          hothidden: false,
          dataList: dataList
        });
      } else {
        wx.showModal({
          title: res.result.errorMessage
        });
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.doLoadData();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;

    // 加载更多 loading
    that.setData({
      hothidden: true
    })

    var page = that.data.page + 1;
    // 获取当前现有数据进行保存
    var list = that.data.dataList;
    var parameter = {};
    parameter['page'] = page;

    http.ApiGet(apiUrl.UrlNoticeList, parameter, function (res) {
      if (res.result.errorCode == 1) {
        var newData = res.data;
        if (newData.length > 0) {
          that.setData({
            page: page,
            hothidden: false,
            dataList: list.concat(newData.map(function (item) {
              
              let start_time = (new Date().getTime() / 1000) - item.start_time;

              if (start_time < 0) {
                item.start_time_text += '(未开始)';
              }
  
              let end_time = (new Date().getTime() / 1000) - item.end_time;
              if (end_time > 0) {
                item.end_time_text += '(已结束)';
              }
              return item;
            }))
          });
        } else {
          that.setData({
            hothidden: false
          })
        }
      } else {
        wx.showModal({
          title: res.result.errorMessage
        });
      }
    });
  },
  //列表点击
  listCellTap: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var item = that.data.dataList[index];
    console.log(item.admin_id);
    var link = "https://v.sogx.cn/wap/main/index/id/" + item.admin_id;
    wx.navigateTo({
      url: '../webview/index?url=' + link
    })
  },
  editTap: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var item = that.data.dataList[index];
    console.log(item.id);
    wx.setStorageSync("noticeData", item);
    wx.navigateTo({
      url: '../member/noticeSubmit?title=编辑通知&adminID=' + item.admin_id + '&id=' + item.id
    })
  },
  delTap: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var item = that.data.dataList[index];
    console.log(item.id);

    var formData = {};
    formData['id'] = item.id;

    wx.showModal({
      title: '确定删除？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定');
          http.ApiPost(apiUrl.UrlNoticeDel, formData, function (res) {
            if (res.result.errorCode == 1) {
              that.doLoadData();
            } else {
              wx.showModal({
                title: res.result.errorMessage
              });
            }
          });
        }
      }
    });
  },
  addBtnTap: function () {
    wx.navigateTo({
      url: '../member/noticeSubmit?title=新建通知'
    })
  }
})